package com.lanju.task;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.lanju.common.WxMessageUtil;
import com.lanju.dao.AllFreeDao;
import com.lanju.dao.UserDao;
import com.lanju.entity.AllFree;
import com.lanju.entity.User;
import com.lanju.service.UserService;
/**
 * 费用定时任务
 * @author zengqd
 * @createTime 2019年9月16日下午4:38:10
 */
@Component
public class FeeJob {
	
	@Autowired
	AllFreeDao allFreeDao;
	@Autowired
	UserDao userDao;
	
    @Autowired
    UserService userService;
    
    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    
	@SuppressWarnings("unused")
//	@Scheduled(cron="0 0 0 25 * ? ") //间隔5秒执行
//	@Scheduled(cron="0 1/2 * * * ? ") //间隔5秒执行
	public void sendMsg() throws ClientProtocolException, IOException{
		System.out.println("执行了");
		Map<String,Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		map.put("month", sdf.format(new Date()));
		List<AllFree> list= allFreeDao.getAllFreeList(map);
		if(list != null && list.size()>0){
			for(int i=0;i<list.size();i++){
				AllFree al= list.get(i);
				String remark = fillRemark(al);
				System.out.println(remark);
				int count = countPrice(al);
				User u = userDao.getById(al.getUserId());
				if(!StringUtils.isEmpty(u.getOpenId())){
					String data = fillData("x1XmbKmGDV_1Hd3bSCiheu1EiS86OxQLdRT5_XPu6zM", sdf.format(new Date())+"月账单", al.getUserName(), al.getHouseAddr(), "每月", count, "暂无", u.getOpenId(), remark);	
					Map<String,Object> result1 = WxMessageUtil.contractPayMesgJob(data);
					System.out.println("发送返回结果："+result1);
				}
				//管理员
				Map<String,Object> param = new HashMap<>();
				param.put("reviceType", "租金缴费提醒");
				List<User> userList = userService.getSendMessageUser(param);
				if(list != null && list.size()>0){
					for(int j=0;j<list.size();j++){
						User user =  userList.get(j);
						remark = "请及时处理";
						String data = fillData("x1XmbKmGDV_1Hd3bSCiheu1EiS86OxQLdRT5_XPu6zM", sdf.format(new Date())+"月账单", al.getUserName(), al.getHouseAddr(), "每月", count, "暂无", user.getOpenId(), remark);	
						Map<String,Object> result1 = WxMessageUtil.contractPayMesgJob(data);
						System.out.println("发送返回结果："+result1);
					}
				}
			}
		}
		System.out.println("==="+list);
	}

	private String fillRemark(AllFree al) {
		StringBuffer remark = new StringBuffer();
		if(!StringUtils.isEmpty(al.getRent())){
			remark.append("租金:" + al.getRent()+"；");
		}
		if(!StringUtils.isEmpty(al.getElectricity())){
			remark.append("电费:" + al.getElectricity()+"；");
		}
		if(!StringUtils.isEmpty(al.getGas())){
			remark.append("天然气费:" + al.getGas()+"；");
		}
		if(!StringUtils.isEmpty(al.getManager_fee())){
			remark.append("物业管理费:" + al.getManager_fee()+"；");
		}
		if(!StringUtils.isEmpty(al.getNetword())){
			remark.append("网络费用:" + al.getNetword()+"；");
		}
		if(!StringUtils.isEmpty(al.getWater())){
			remark.append("水费:" + al.getWater()+"；");
		}
		if(!StringUtils.isEmpty(al.getOther_fee())){
			remark.append("其他费用:" + al.getOther_fee()+"；");
		}
		return remark.toString();
	}
	private int countPrice(AllFree al) {
		int count = 0;
		if(!StringUtils.isEmpty(al.getRent())){
			count += al.getRent();
		}
		if(!StringUtils.isEmpty(al.getElectricity())){
			count += al.getElectricity();
		}
		if(!StringUtils.isEmpty(al.getGas())){
			count += al.getGas();
		}
		if(!StringUtils.isEmpty(al.getManager_fee())){
			count += al.getManager_fee();
		}
		if(!StringUtils.isEmpty(al.getNetword())){
			count += al.getNetword();
		}
		if(!StringUtils.isEmpty(al.getWater())){
			count += al.getWater();
		}
		if(!StringUtils.isEmpty(al.getOther_fee())){
			count += al.getOther_fee();
		}
		return count;
	}
	
	/**
	 * 
	 * 作者：zengqd
	 * @param templateId
	 * @param first   标题
	 * @param keyword1 客户名称
	 * @param keyword2 承租房间
	 * @param keyword3 缴费周期
	 * @param count 缴费总额
	 * @param keyword5  指定账号
	 * @param remark  备注
	 * @return
	 */
	public String fillData (String templateId,String first,String keyword1,String keyword2,String keyword3,int count,String keyword5,String openid,String remark){
		String data="{\"touser\":\""+openid+"\",\"template_id\":\""+templateId+"\",\"topcolor\":\"#FF0000\", "
				+ "\"data\":{\"first\": { \"value\":\""+first+"\",\"color\":\"#173177\"},"
						+ "\"keyword1\":{\"value\":\""+keyword1+"\",\"color\":\"#173177\" },"
								+ "\"keyword2\": { \"value\":\""+keyword2+"\",\"color\":\"#173177\"},"
										+ "\"keyword3\":{\"value\":\""+keyword3+"\",\"color\":\"#173177\"}, "
										+ "\"keyword4\":{\"value\":\""+count+"\",\"color\":\"#173177\"}, "
										+ "\"keyword5\":{\"value\":\""+keyword5+"\",\"color\":\"#173177\"}, "
												+ "\"remark\":{\"value\":\""+remark+"\",\"color\":\"#173177\"}"
														+ "}}";
		return data;
	}
}
